//
//  ExtensionClass.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/20.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import Foundation
import UIKit
import YYModel

extension UIViewController {

    // MARK: 设置错误提示框
    public func showErrorAlert(title :String? = "网络请求失败",msg:String? = "") {
        var errorStr = title
        if msg != "" {
            errorStr = global_requestFailed
        }
        KMTool.setAlertView(title:errorStr, msg:msg, target: self, haveCancel: false, handler: nil)
    }
    
    //MARK: 判断页面是否显示
    public func isCurrentViewControllerVisible(viewcontroller:UIViewController) -> Bool {
        return viewcontroller.isViewLoaded && (viewcontroller.view.window != nil)
    }

}

// MARK: - NSObject
extension NSObject {
    class func setModel(with dict:[String:Any]) -> Self {
        return self.yy_model(with: dict) ?? self.init()
    }
}

extension UIColor {
    
    class func lineColor() -> UIColor {
        
        return UIColor.lightGray.withAlphaComponent(0.5)
    }
    
    class func pinkColor() -> UIColor {
        
        return UIColor.init(red: 201.0/255, green: 88.0/255, blue: 80.0/255, alpha: 1)
    }
    
    class func tabBarColor() -> UIColor {
        
        return UIColor.init(red: 195.0/255, green: 93.0/255, blue: 84.0/255, alpha: 1)
    }
    
    class func fontGray() -> UIColor {
        return UIColor.gray
    }
    
    class func cyanGreen() -> UIColor {
        
        return UIColor.setRGB(0x56C350)
    }
    
    class func signBgColor() -> UIColor {
        return UIColor.setRGB(0x99dfdbdb)
    }
    
    class func mineColor() -> UIColor {
        return setRGB(0xf0f0f0)
    }
    
    class func verifyColor() -> UIColor {
        return setRGB(0xdddddd)
    }
    
    class func editColor() -> UIColor {
        return setRGB(0xf2f2f2)
    }
    
    class func marqueeColor() -> UIColor {
        return setRGB(0xfae8ca)
    }
    
    class func moveColor() -> UIColor {
        return setRGB(0xF69796)
    }
    
    class func recordYellow() -> UIColor {
        return setRGB(0xf2ba1e)
    }
    
    class func recordBlue() -> UIColor {
        return setRGB(0x1fa7b2)
    }
    
    class func recordGreen() -> UIColor {
        return setRGB(0x99ae20)
    }
    
    class func recordRed() -> UIColor {
        return setRGB(0xe96128)
    }
    
    class func startBlue() -> UIColor {
        return setRGB(0x00EEFF)
    }
    
    class func centerColor() -> UIColor {
        return setRGB(0x00B2FF)
    }
    
    class func endColor() -> UIColor {
        return setRGB(0x0088FF)
    }
    
    class func phoneColor() -> UIColor {
        return setRGB(0x6699cc)
    }
    
    // MARK: 十六进制转rgb
    class func setRGB(_ rgbValue:UInt) -> UIColor {
        return UIColor.init(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, blue: CGFloat(rgbValue & 0x0000FF) / 255.0, alpha: 1)
    }
}

// MARK: - UIImageView
extension UIImageView {
    public func setUrlImage(with urlStr:String? ,placeholder: UIImage?){
        
        self.sd_setImage(with: URL.init(string: urlStr ?? ""), placeholderImage: placeholder)
    }
}

extension UIImage {
    
    //MARK: 颜色转图片
    class func imageWithColor( _ color:UIColor) -> UIImage {
        
        let rect = CGRect.init(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image!
        
    }
    
    //MARK: 加载webp图片
    class func imageWithName( _ imageName:String) -> UIImage {
        
        let path = Bundle.main.path(forResource: imageName, ofType: "webp")
        let data = NSData.init(contentsOfFile: path!)
        
        return UIImage.sd_image(with: data! as Data)!
    }
}

//MARK: String
extension String {
    
    /**
     获取文本范围
     */
    public func size(font : CGFloat ,  width:CGFloat) -> CGSize{
        let size = NSString.init(string: self).boundingRect(with: .init(width: width, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: font)], context: nil).size
        return CGSize.init(width: size.width + 10, height: size.height + 10)
    }
    
    public func size(font : CGFloat ,  height :CGFloat) -> CGSize{
        let size = NSString.init(string: self).boundingRect(with: .init(width: CGFloat.greatestFiniteMagnitude, height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: font)], context: nil).size
        return CGSize.init(width: size.width + 10, height: size.height + 10)
    }
    
    //MARK: 时间戳转时间
    public func getDate() -> String {
        
        let date = Date.init(timeIntervalSince1970: Double(self)!)
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let dateString = formatter.string(from: date)
        
        return dateString
    }
}














